package net.wangds.yunz.auth.handler;

import net.wangds.utils.JsonUtils;
import net.wangds.web.entrypoint.BaseResp;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {

    /**
     * Commences an authentication scheme.
     * <p>
     * <code>ExceptionTranslationFilter</code> will populate the <code>HttpSession</code>
     * attribute named
     * <code>AbstractAuthenticationProcessingFilter.SPRING_SECURITY_SAVED_REQUEST_KEY</code>
     * with the requested target URL before calling this method.
     * <p>
     * Implementations should modify the headers on the <code>ServletResponse</code> as
     * necessary to commence the authentication process.
     *
     * @param request  that resulted in an <code>AuthenticationException</code>
     * @param response so that the user agent can begin authentication
     * @param ex       that caused the invocation
     */
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException ex) throws IOException {
        //TODO处理认证异常
        //LogHelper.dev(()->"处理认证异常", authException);



        response.sendError(HttpStatus.UNAUTHORIZED.value(), JsonUtils.toJsonStr(
                BaseResp.createDefault().code(-1).msg(ex.getLocalizedMessage())
        ));
        //LogHelper.dev(()->"处理认证异常", ex);


    }
}